<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
	<th:block th:include="include :: header('导入表结构')" />
</head>
<body>
<div class="layui-fluid">
	<div class="layui-card">
		<!--search form-->
		<form class="layui-form layui-card-header layuiadmin-card-header-auto search-form" id="searchForm">
			<div class="layui-form-item">
				<div class="layui-inline">
					<label class="layui-form-label">表名称</label>
					<div class="layui-input-inline">
						<input class="layui-input" type="text" name="tableName" placeholder="" autocomplete="off">
					</div>
				</div>
				<div class="layui-inline">
					<label class="layui-form-label">表描述</label>
					<div class="layui-input-inline">
						<input class="layui-input" type="text" name="tableComment" placeholder="" autocomplete="off">
					</div>
				</div>
				<div class="layui-inline">
					<button  type="button" class="layui-btn layuiadmin-btn-list" data-type="reload" lay-submit lay-filter="form-search" id="form-search"><i class="layui-icon">&#xe615;</i>搜索</button>
				</div>
			</div>
		</form>
		<!--table actcion-->
		<div class="layui-card-body">
			<table id="table_import" lay-filter="table_import"></table>
		</div>
	</div>
</div>
<th:block th:include="include :: footer"/>
<script>
	var prefix = ctx + "tool/gen";
	layui.use(['common'], function () {
		var $ = layui.jquery,
				form = layui.form,
				common = layui.common,
				table = layui.table;
		//页面事件列表
		var active = {
			reload: function () {
				search(false);
			},
			remove: function (ids) {
				common.ajaxRemove(prefix + "/remove", ids, function (res) {
					active['reload'].call(this);
				});
			}
		};

		//查询表单
		form.on('submit(form-search)', function (data) {
			search(true);
		});

		//表格初始化
		table.render({
			elem: '#table_import'
			, method: 'post'
			, url: prefix + "/db/list" //数据接口
			, defaultToolbar:[]
			, autoSort: false 	//关闭前端排序 使用后端排序
			, limit: getPageSize()
			, limits: getPageList()
			, text: getLoadErrorMsg()
			, page: true
			, cols: new Array([
				{type: 'checkbox', width: 40}
				, {field: 'tableName', title: '表名称'}
				, {field: 'tableComment', title: '表描述'}
				, {field: 'createTime', title: '创建时间', width: 160, sort: true}
				, {field: 'updateTime', title: '更新时间', width: 160}
			])
			,parseData: function(res){ //res 即为原始返回的数据
				return {
					"code": res.code, //解析接口状态
					"msg": res.msg, //解析提示文本
					"count": res.total, //解析数据长度
					"data": res.rows //解析数据列表
				};
			}
		});
		//监听排序事件
		table.on('sort(table_import)', function (obj) {
			var field = serializeJson($('#searchForm'));
			field.orderByColumn = obj.field ;
			field.isAsc = obj.type;
			table.reload('table_import', {where: field,page:{curr: 1}});//执行重载
		});
	});

	//表格查询
	function search(isResetCurr) {
		var field = serializeJson($('#searchForm'));
		var sortParam = getSort('table_import');
		if (sortParam) {
			field.orderByColumn = sortParam.sortField;
			field.isAsc = sortParam.sortMethod;
		}
		//执行重载
		var options = {where: field};
		if (isResetCurr == true) {
			options.page = {curr: 1};
		}
		layui.table.reload('table_import', options);
	}
	/* 导入表结构-选择表结构-提交 */
	function submitHandler() {
		var checks = layui.table.checkStatus('table_import');
		if (checks.data.length == 0) {
			layui.layer.alert("请至少选择一条记录");
			return;
		}
		var tables = layui.common.joinArray(checks.data, "tableName");
		var data = {"tables": tables};
		layui.common.ajax.post(prefix + "/importTable",data,function(res){
			layui.layer.alert(res.msg);
			if(res.code==0){
				parent.layer.closeAll();
				if(parent.search){
					parent.search();
				}else{
					parent.layui.table.reload();
				}

			}
		});
	}
</script>
</body>
</html>